// eslint-disable-next-line @typescript-eslint/no-var-requires
const bcrypt = require('bcrypt');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require('fs');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');

// 配置用户名和密码
const username = 'admin';
const password = 'admin'; // 替换为你的密码
const rounds = 10;

// 生成bcrypt哈希
bcrypt.hash(password, rounds, (err, hash) => {
  if (err) {
    console.error('生成哈希失败:', err);
    process.exit(1);
  }

  // 构建htpasswd格式内容
  const content = `${username}:${hash.replace('$2b$', '$2y$')}\n`;

  // 确保目录存在
  const dir = path.join(__dirname, 'verdaccio', 'conf');
  if (!fs.existsSync(dir)) {
    fs.mkdirSync(dir, { recursive: true });
  }

  // 写入文件
  const filePath = path.join(dir, 'htpasswd');
  fs.writeFileSync(filePath, content, 'utf8');
  console.log(`已生成htpasswd文件: ${filePath}`);
});
